import java.util.Iterator;

public class LinkListDoubleQue {
    public static void main(String[] args) {
        LinkedListDeque<Integer> linkedListDeque = new LinkedListDeque<>(5);
        linkedListDeque.offerFirst(2);
        linkedListDeque.offerFirst(4);
        linkedListDeque.offerFirst(6);
        linkedListDeque.offerFirst(8);
        linkedListDeque.pollFirst();
        linkedListDeque.pollLast();
        linkedListDeque.offerLast(10);
        for (Integer i:linkedListDeque){
            System.out.println(i);
        }
    }
}

class LinkedListDeque<E> implements Deque<E>,Iterable<E>{
    int size;
    int capecity;
    Node<E> sential = new Node<>(null,null,null);

    @Override
    public boolean offerFirst(E e) {
        if (isFull()){
            return false;
        }
        Node<E> b = sential.next;
        Node<E> add = new Node<>(sential,e,sential.next);
        sential.next = add;
        b.prev = add;
        size++;
        return true;
    }

    @Override
    public boolean offerLast(E e) {
        if (isFull()){
            return false;
        }
        Node<E> b = sential.prev;
        Node<E> add = new Node<>(b,e,sential);
        b.next = add;
        sential.prev = add;
        size++;
        return true;
    }

    @Override
    public E pollFirst() {
        if (isEmpty()){
            return null;
        }
        Node<E> a = sential;
        Node<E> polled = sential.next;
        Node<E> b = polled.next;
        a.next = b;
        b.prev = a;
        size--;
        return polled.value;
    }

    @Override
    public E pollLast() {
        if (isEmpty()){
            return null;
        }
        Node<E> polled = sential.prev;
        Node<E> a = polled.prev;
        Node<E> b = sential;
        a.next = b;
        b.prev = a;
        size--;
        return polled.value;
    }

    @Override
    public E peekFirst() {
        if (isEmpty()){
            return null;
        }
        return sential.next.value;
    }

    @Override
    public E peekLast() {
        if (isEmpty()){
            return null;
        }
        return  sential.prev.value;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean isFull() {
        return size == capecity;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            Node<E> p =sential.next;
            @Override
            public boolean hasNext() {
                return p != sential;
            }

            @Override
            public E next() {
                E val = p.value;
                p = p.next;
                return val;
            }
        };
    }

    class Node<E>{
        Node<E> prev;
        E value;
        Node<E> next;

        public Node(Node<E> prev,E value,Node<E> next){
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    public LinkedListDeque(int capecity){
        this.capecity = capecity;
        sential.next = sential;
        sential.prev = sential;
    }
}
